package net.zcy.springbootmybatis.handle;

import net.zcy.springbootmybatis.exception.ResultException;
import net.zcy.springbootmybatis.output.Result;
import net.zcy.springbootmybatis.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionHandle {

    private static Logger LOG = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    public Result handle(Exception e){

        if(e instanceof ResultException){
            ResultException exception = (ResultException) e;
            return ResultUtil.error(exception.getCode(),exception.getMessage());
        }else {
            LOG.error("【系统异常：】 {}",e);
            return ResultUtil.sysError();
        }
    }
}
